package com.eclab.springbootgo.controller;

import com.eclab.springbootgo.model.User;
import com.eclab.springbootgo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 *
 * @author Aiden <a href="mailto:adam602260@gmail.com"/>
 * @version UserController.java v0.1
 * @create 2019-08-28 10:20
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public String register(User user){
        try {
            log.info("【register】:save user {}", user);
            userService.save(user);
        }catch (Exception e){
            log.error("【register】error: {}", e.getMessage());
            return e.getMessage();
        }
        return "register success!";
    }

    @PostMapping("/signin")
    public String signin(User user){

        User existUser = null;
        try {
            existUser = userService.getUser(user.getUsername());
            if (existUser == null){
                log.info("【signin】: user not found");
                return "user not found";
            }
            if (!user.getPassword().equals(existUser.getPassword())){
                log.info("【signin】: password error");
                return "password error";
            }
        }catch (RuntimeException e){
            log.error("【signin】error: {}", e.getMessage());
            return  e.getMessage();
        }
        log.info("【signin】: signin success!");
        return "signin success!";
    }


}